package cn.csg.ams.collect2.poptopics.dataHandle;


import cn.csg.ams.collect2.poptopics.datahub.TupleRecordDataUtils;
import cn.csg.ams.collect2.poptopics.entity.RecordInfo;
import cn.csg.ams.collect2.poptopics.redisPool.OutagePool;
import cn.csg.ams.collect2.poptopics.service.MkMcTermAlarmLogService;
import com.aliyun.datahub.client.model.TupleRecordData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 表码消费处理类
 */
@Component
public class MeterReadHandle {

    private static Logger logger = LoggerFactory.getLogger(MeterReadHandle.class);

    @Autowired
    private OutagePool outagePool;

    @Autowired
    private MkMcTermAlarmLogService alarmLogService;

    /**
     * 停复电处理类
     */
    @Autowired
    private OutgaeAndRecover outgaeAndRecover;


    public void handle(TupleRecordData data) {
        // 运行电能表标识
        String runMeterId = TupleRecordDataUtils.getStringField(data, "run_meter_id");
        // 地区编码
        String areaCode = TupleRecordDataUtils.getStringField(data, "area_code");

        // 查询表码对应电表是否停电
        if (!outagePool.isOutage(runMeterId, areaCode)) {
            return;
        }

        logger.info("开始通过表码值复电。。。。。");
        long startTimes = System.currentTimeMillis();

        // 查询对应档案信息
        RecordInfo recordInfo = alarmLogService.getRecordInfo(runMeterId, areaCode);
        if (recordInfo == null) {
            // 未查询到档案信息，直接返回
            return;
        }

        // 复电时间
        String outageEndTime = TupleRecordDataUtils.getStringField(data, "data_time");
        outgaeAndRecover.otherRecover(outageEndTime, "6", recordInfo, startTimes);
    }

}
